/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

/*
 * PanelPreferencesSecurity.java
 *
 * Created on Sep 10, 2011, 11:51:19 PM
 */
package group.pals.mailgate.preferences.ui;

import group.pals.mailgate.ui.IPreferences;
import group.pals.mailgate.preferences.Preferences;
import group.pals.mailgate.utils.Utils;
import group.pals.utils.sys.MessageBox;
import java.util.Arrays;

/**
 *
 * @author Hai Bison
 */
public class PanelPreferencesSecurity extends javax.swing.JPanel implements IPreferences {

    private final Utils _Utils;
    private final group.pals.mailgate.ui.Utils UIUtils;
    private final Preferences _Preferences;

    /** Creates new form PanelPreferencesSecurity */
    public PanelPreferencesSecurity() {
        initComponents();

        _Utils = Utils.getInstance();
        UIUtils = group.pals.mailgate.ui.Utils.getInstance();
        _Preferences = Preferences.getInstance();

        initUI();
    }

    private void initUI() {
        txtPassword.setComponentPopupMenu(UIUtils.getEditorPopupMenu());
        txtConfirmPassword.setComponentPopupMenu(UIUtils.getEditorPopupMenu());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroupAuthentication = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        btnUseMasterPassword = new javax.swing.JRadioButton();
        btnUseCurrentUser = new javax.swing.JRadioButton();
        txtPassword = new javax.swing.JPasswordField();
        txtConfirmPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Authentication:"));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        buttonGroupAuthentication.add(btnUseMasterPassword);
        btnUseMasterPassword.setFont(new java.awt.Font("Dialog", 0, 12));
        btnUseMasterPassword.setText("Use master password");
        btnUseMasterPassword.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                btnUseMasterPasswordItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(btnUseMasterPassword, gridBagConstraints);

        buttonGroupAuthentication.add(btnUseCurrentUser);
        btnUseCurrentUser.setFont(new java.awt.Font("Dialog", 0, 12));
        btnUseCurrentUser.setText("Use current user");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(btnUseCurrentUser, gridBagConstraints);

        txtPassword.setBorder(javax.swing.BorderFactory.createTitledBorder("Password:"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(txtPassword, gridBagConstraints);

        txtConfirmPassword.setBorder(javax.swing.BorderFactory.createTitledBorder("Confirm:"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(txtConfirmPassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel1, gridBagConstraints);

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel1.setText("<html><b>Note:</b> The option \"Use current user\" is not safe in any case.<br>\n<b>Use it at your own risk.</b></html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weighty = 1.0;
        add(jLabel1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

  private void btnUseMasterPasswordItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_btnUseMasterPasswordItemStateChanged
      final boolean IsSelected = btnUseMasterPassword.isSelected();
      txtPassword.setEnabled(IsSelected);
      txtConfirmPassword.setEnabled(IsSelected);
  }//GEN-LAST:event_btnUseMasterPasswordItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton btnUseCurrentUser;
    private javax.swing.JRadioButton btnUseMasterPassword;
    private javax.swing.ButtonGroup buttonGroupAuthentication;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField txtConfirmPassword;
    private javax.swing.JPasswordField txtPassword;
    // End of variables declaration//GEN-END:variables

    /*----------------------------------------------------------------------------
     * IPreferences
     */
    public void loadPreferences() {
        final boolean IsUseMasterPassword = _Preferences.isUseMasterPassword();

        btnUseCurrentUser.setSelected(!IsUseMasterPassword);
        btnUseMasterPassword.setSelected(IsUseMasterPassword);

        if (IsUseMasterPassword) {
            txtPassword.setText(_Preferences.getMasterPassword());
            txtConfirmPassword.setText(_Preferences.getMasterPassword());
        }

        txtPassword.setEnabled(IsUseMasterPassword);
        txtConfirmPassword.setEnabled(IsUseMasterPassword);
    }

    public boolean storePreferences(boolean prepare) {
        if (btnUseMasterPassword.isSelected()) {
            if (!Arrays.equals(txtPassword.getPassword(), txtConfirmPassword.getPassword())) {
                MessageBox.showErrMsg(this, null, "Password does not match");
                return false;
            }
        }

        if (prepare) {
            return true;
        }

        //always set master password first
        if (btnUseCurrentUser.isSelected()) {
            _Preferences.setMasterPassword(Utils.JvmUniqueID + _Utils.getUsername());
            _Preferences.setUseMasterPassword(false);
            _Preferences.setAuthUsername(_Utils.getUsername());
        } else {
            _Preferences.setMasterPassword(new String(txtPassword.getPassword()));
            _Preferences.setUseMasterPassword(true);
            _Preferences.setAuthUsername("");
        }

        return true;
    }

    public void closeResources() {
        //do nothing
    }
}
